<template>
    <div>
        <div style="margin-bottom: 10px;margin-left:5px" >
            <Row>
            <Button type="primary" @click="btn_refresh" style="margin-right:10px;margin-bottom:10px" >刷新</Button>
            <Button type="primary" @click="btn_stop_timer"  style="margin-right:10px;margin-bottom:10px">停止刷新</Button>
            <Button type="primary" @click="btn_get_problem_deployments"  style="margin-right:10px;margin-bottom:10px">故障部署</Button>
            <Dropdown>
                <a href="javascript:void(0)">
                    <Icon type="ios-create-outline" size="40" />
                </a>
                <DropdownMenu slot="list" placement="bottom-end">
                    <DropdownMenu><a class="button-link"  @click="btn_update_replicas" >扩缩容</a></DropdownMenu>
                    <DropdownMenu><a class="button-link"  @click="btn_add_toleration" >添加容忍</a></DropdownMenu>
                    <DropdownMenu><a class="button-link"  @click="btn_add_node_affinity" >添加节点亲和性</a></DropdownMenu>
                    <DropdownMenu><a class="button-link"  @click="btn_add_pod_anti_affinity" >添加Pod互斥调度</a></DropdownMenu>
                </DropdownMenu>
            </Dropdown>
            <Dropdown>
                <a href="javascript:void(0)">
                    <Icon type="ios-trash" size="40" />
                </a>
                <DropdownMenu slot="list" placement="bottom-end">
                    <DropdownMenu><a class="button-link"   @click="btn_del_deploy" >删除Deploy</a></DropdownMenu>
                    <DropdownMenu><a class="button-link"   @click="btn_del_toleration" >删除容忍</a></DropdownMenu>
                    <DropdownMenu><a class="button-link"   @click="btn_del_node_affinity" >删除节点亲和性</a></DropdownMenu>
                    <DropdownMenu><a class="button-link"   @click="btn_del_pod_anti_affinity" >删除Pod互斥调度</a></DropdownMenu>
                    
                </DropdownMenu>
            </Dropdown>
            </Row>
        </div>
        <i-table border stripe  ref="deployment_table" @on-selection-change="onSelect"  :columns="format" :data="show_list" :height="sharedState.tableHeight" >
            <!-- <template slot-scope="{ row, index }" slot="action">
                <Button  type="primary"  @click="btn_edit_deploy(index)"> 编辑</Button>
            </template> -->
        </i-table >
        <div style="text-align: center;margin-top: 10px;">
            <Page ref="page" :total="total" :page-size="pageSize"  @on-change="changePage" show-total/>
        </div>
        <!-- 添加容忍模态框  -->
        <Modal v-model="show_add_toleration_modal" width="600" draggable>
            <p slot="header" style="color:#f60;text-align:center">
                {{ deploy_name }}<span>增加容忍</span>
            </p>
            <div style="">
                <Form ref="toleration" :model="toleration" :rules="toleration_rules" label-position="left"  :label-width="150">
                    <Form-Item label="选择容忍效果" prop="effect">
                        <Select v-model="toleration.effect">
                            <Option v-for="effect in toleration_effect_list" :key="effect" :value="effect">{{ effect }}</Option>
                        </Select>
                    </Form-Item> 
                    <Form-Item label="key:"  prop="key">
                        <Input type="text"  v-model="toleration.key" />
                    </Form-Item>
                    <Form-Item label="选择operator:" prop="operator">
                        <Select v-model="toleration.operator">
                            <Option v-for="operator in toleration_operator_list" :key="operator" :value="operator">{{ operator }}</Option>
                        </Select>
                    </Form-Item> 
                    <Form-Item label="value:" prop="value" >
                        <Input type="text" v-if="toleration.operator!='Exists'" v-model="toleration.value"  />
                        <Input type="text" v-else placeholder="operator为Exists不需要value"  disabled/>
                    </Form-Item>
                    <Form-Item label="tolerationSeconds:" prop="tolerationSeconds">
                        <Input type="number" v-if="toleration.effect=='NoExecute'" v-model="toleration.tolerationSeconds"  />
                        <Input type="number" v-else placeholder="只有effect为NoExecute才需要填写"  disabled  />
                    </Form-Item>
                </Form> 
               
            </div>
            <div slot="footer">
                 <p style="text-align: left"> 说明tolerationSeconds:配合NoExecute使用，允许pod保留多久</p>
                <Button type="primary" size="large"  @click="submit_add_toleration">提交</Button>
            </div>
        </Modal>
        <!-- 删除容忍模态框 -->
        <Modal v-model="show_del_toleration_modal" width="800" draggable>
            <p slot="header" style="color:#f60;text-align:center;">
                {{ deploy_name }}<span>删除容忍</span>
            </p>
            <div style="">
                <Form>
                    <Form-Item >
                        <Radio-Group v-model="toleration_del_index" vertical>
                            <Radio v-for="(toleration,index) in tolerations" :label="index" :key="index">{{ toleration }}</Radio>
                        </Radio-Group>
                    </Form-Item> 
                </Form> 
            </div>
            <div slot="footer">
                <Button type="primary" size="large"  @click="submit_del_toleration">提交</Button>
            </div>
        </Modal>
        <!-- 添加节点亲和性模态框 -->
        <Modal v-model="show_add_node_affinity_modal" width="800" draggable>
            <p slot="header" style="color:#f60;text-align:center">
                {{ deploy_name }}<span>添加节点亲和性</span>
            </p>
            <div style="">
                <Form ref="node_affinity" :model="node_affinity" :rules="node_affinity_rules" label-position="left"  :label-width="150">
                    <Form-Item label="选择亲和性类型" prop="type">
                        <Select v-model="node_affinity.type">
                            <Option v-for="type in affinity_types" :key="type" :value="type">{{ type }}</Option>
                        </Select>
                    </Form-Item> 
                    <Form-Item label="选择节点选择器类型" prop="match_type">
                        <Select v-model="node_affinity.match_type">
                            <Option v-for="match_type in node_selector_types" :key="match_type" :value="match_type">{{ match_type }}</Option>
                        </Select>
                    </Form-Item> 
                    <Form-Item label="key:"  prop="key">
                        <Input type="text"  v-model="node_affinity.key" />
                    </Form-Item>
                    <Form-Item label="选择operator:" prop="operator">
                        <Select v-model="node_affinity.operator">
                            <Option v-for="operator in node_affinity_operators" :key="operator" :value="operator">{{ operator }}</Option>
                        </Select>
                    </Form-Item> 
                    <Form-Item label="value:" prop="value" >
                        <Input type="text" v-if="node_affinity.operator!='Exists' && node_affinity.operator!='DoesNotExist' " v-model="node_affinity.value"  />
                        <Input type="text" v-else placeholder="operator为Exists/DoesNotExist不需要value"  disabled/>
                    </Form-Item>
                    <Form-Item label="weight:" prop="weight">
                        <Input type="number" v-if="node_affinity.type=='preferred'" v-model="node_affinity.weight"  />
                        <Input type="number" v-else placeholder="preferred亲和类型才需要填写"  disabled  />
                    </Form-Item>
                </Form> 
               
            </div>
            <div slot="footer">
                 <p style="text-align: left">说明1: matchFields:匹配field-selector 例如:metadata.name: 192.168.11.54</p>
                 <p style="text-align: left">说明2: value: must be specified single value when `operator` is 'Lt' or 'Gt'"</p>
                 <p style="text-align: left">说明3: values: Required value: must be only one value when `operator` is 'In' or 'NotIn' for node field selector</p>
                 <p style="text-align: left">说明4: key: Invalid value: "server": not a valid field selector key"</p>
                 <p style="text-align: left">说明5: 只支持添加一项节点亲和性，再添加会覆盖前面添加的"</p>
                 <!-- submit_add_node_affinity('node_affinity')这里是表单的 ref值-->
                <Button type="primary" size="large"  @click="submit_add_node_affinity('node_affinity')">提交</Button>
            </div>
        </Modal>
        <!-- 添加POD互斥模态框 -->
        <Modal v-model="show_add_pod_anti_affinity_modal" width="800" draggable>
            <p slot="header" style="color:#f60;text-align:center">
                {{ deploy_name }}<span>添加POD互斥调度</span>
            </p>
            <div style="">
                <Form ref="pod_anti_affinity" :model="pod_anti_affinity" :rules="pod_anti_affinity_rules" label-position="left"  inline :label-width="150">
                    <Form-Item label="互斥类型:" prop="type" style="width:350px">
                        <Select v-model="pod_anti_affinity.type">
                            <Option v-for="type in affinity_types" :key="type" :value="type">{{ type }}</Option>
                        </Select>
                    </Form-Item> 
                    <Form-Item label="标签选择器类型:" prop="match_type" style="width:350px">
                        <Select v-model="pod_anti_affinity.match_type">
                            <Option v-for="match_type in label_selector_types" :key="match_type" :value="match_type">{{ match_type }}</Option>
                        </Select>
                    </Form-Item> 
                    <Form-Item label="key:"  prop="key" style="width:350px">
                        <Input type="text"  v-model="pod_anti_affinity.key" />
                    </Form-Item>
                    <Form-Item label="选择operator:" prop="operator" style="width:350px">
                        <Select v-model="pod_anti_affinity.operator" >
                            <Option v-for="operator in pod_anti_affinity_operators" :key="operator" :value="operator">{{ operator }}</Option>
                        </Select>
                    </Form-Item> 
                    <Form-Item label="value:" prop="value" style="width:350px" >
                        <Input type="text" v-if="pod_anti_affinity.operator!='Exists' && pod_anti_affinity.operator!='DoesNotExist' " v-model="pod_anti_affinity.value"  />
                        <Input type="text" v-else placeholder="operator为Exists/DoesNotExist不需要value"  disabled/>
                    </Form-Item>
                    <Form-Item label="weight:" prop="weight" style="width:350px">
                        <Input type="number" v-if="pod_anti_affinity.type=='preferred'" v-model="pod_anti_affinity.weight"  />
                        <Input type="number" v-else placeholder="preferred互斥类型才需要填写"  disabled  />
                    </Form-Item>
                    <Form-Item label="topologyKey:"  prop="topologyKey" style="width:350px">
                        <Input type="text"  v-model="pod_anti_affinity.topologyKey" />
                    </Form-Item>
                </Form> 
               
            </div>
            <div slot="footer">
                <p style="text-align: left">说明1: key value 一般填写 deployment的selector 或者label的键值对</p>
                <p style="text-align: left">说明2: 只支持添加一项pod互斥调度，再添加会覆盖前面添加的"</p>
                <Button type="primary" size="large"  @click="submit_add_pod_anti_affinity('pod_anti_affinity')">提交</Button>
            </div>
        </Modal>

        <!-- 编辑模态框 -->
        <Modal v-model="show_edit_deploy_modal" width="900" draggable>
            <p slot="header" style="color:#f60;">
                <span>编辑资源</span>
            </p>
            <div>
                <yaml-editor  v-model="value"   />
            </div>
            <div slot="footer" style="text-align:center">
                <Button type="primary" size="default"  @click="submit_update_deployment">更新</Button>
                <Button type="primary" size="default"  @click="cacel_update_deploy">取消</Button>
            </div>
        </Modal>
    </div>
</template>

<script>

import {k8s_namespace_post} from '@/api/http'
import {  
    get_deployment_list,
    update_replicas,delete_deploy, 
    add_toleration,
    delete_toleration,
    get_deployment_object,
    get_problem_deployments,
    update_deployment_object,
} from  '@/api'
import { 
    add_node_affinity,
    delete_node_affinity,
    add_pod_anti_affinity,
    delete_pod_anti_affinity
} from  '@/api'
import { delete_namespace_resource,get_placement,show_simple_page } from '@/common/util.js'
import YAML from 'json2yaml';
import YamlEditor  from '@/lib/editor/yamlEditor';
import store from '@/store'

export default {
    components: { YamlEditor },
    data() {
        const validateWeight = (rule,value,callback) => {
            if(value <1 || value > 100) {
                callback(new Error('请输入1-100整数'))
            }else{
                callback();
            }
        };
        return {
            sharedState: store.state,
            format: [
                {
                    type: 'selection',
                    width: 100,
                    align: 'center'
                },
                {
                    title: '名字',key: 'name',
                    render: (h,params) => {
                        return h('a',{
                                style: {
                                    color: 'blue'
                                },
                                on: {
                                    click: ()=>{                       
                                        this.handleLink(params);
                                    }
                                }
                            },params.row.name)
                        
                    }
                },
                {
                    title: '命名空间',key: 'namespace'
                },
                {
                    title: 'pods',key: 'pods',
                },
                {
                    title: '标签',key: 'labels',width:250,
                    render: (h, params) => {
                        let labels = params.row.labels
                        let val = ''
                        for(let key in labels){
                            val = val+ key+":"+labels[key]+"\n"
                        }
                        if(labels == null){
                            return h('p','无')
                        }else{

                            let len = val.split('\n').length -1
                            if(len > 0 && len < 3){
                                return h('pre',val)
                            }else{
                                let placement = get_placement(params)
                                return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:val}},[
                                        h('a', '显示')
                                ]);
                            }
                        }
                    }
                },
                {
                    title: '容忍',key: 'tolerations',
                   render : (h,params) => {
                        let tolerations = params.row.tolerations 
                        if (tolerations == null ){
                            return   h('p','无')
                        }else{
                            let value = JSON.stringify(tolerations,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }
                    }
                },
                {
                    title: '节点选择',key: 'node_selector',
                   render : (h,params) => {
                        let node_selector = params.row.node_selector 
                        if (node_selector == null ){
                            return   h('p','无')
                        }else{
                            let value = JSON.stringify(node_selector,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }
                    }
                },

                {
                    title: '节点亲和性',key: 'affinify',
                    render : (h,params) => {
                        let affinity  = params.row.affinity
                        if(affinity == null){
                            return h('p','无')
                        }
                        let node_affinity = affinity['nodeAffinity']
                        if (node_affinity == null ){
                            return   h('p','无')
                        }else{
                            let value = JSON.stringify(node_affinity,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }
                    }
                },
                {
                    title: 'Pod亲和性',key: 'affinify',
                    render : (h,params) => {
                        let affinity  = params.row.affinity
                        if(affinity == null){
                            return h('p','无')
                        }
                        let pod_affinity = affinity['podAffinity']
                        if (pod_affinity == null ){
                            return   h('p','无')
                        }else{
                            let value = JSON.stringify(pod_affinity,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }
                    }
                },
                {
                    title: 'Pod互斥调度',key: 'affinify',
                    render : (h,params) => {
                        let affinity  = params.row.affinity
                        if(affinity == null){
                            return h('p','无')
                        }
                        let pod_anti_affinity = affinity['podAntiAffinity']
                        if (pod_anti_affinity == null ){
                            return   h('p','无')
                        }else{
                            let value = JSON.stringify(pod_anti_affinity,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }
                    }
                },
                {
                    title: '更新策略',key: 'strategy',
                    render : (h,params) => {
                        let strategy  = params.row.strategy
                        if(strategy == null){
                            return h('p','无')
                        } else{
                            let value = JSON.stringify(strategy,undefined,4)
                            let placement = get_placement(params)
                            return h('Poptip', {props:{'trigger':'hover','word-wrap':true,'placement':placement,content:value}},[
                                    h('a', '显示')
                            ]);
                        }
                    }
                },
                {
                    title: '镜像',key: 'image',
                    render : (h,params) => {
                        let image  = params.row.image
                        if(image == null){
                            return h('p','无')
                        } else{
                            let temp = image.split('/')
                            let value = temp[temp.length -1 ]
                            return h('p',value)
                        }
                    }
                },
                {
                    title: '创建时间',key: 'create_time'
                },
                // {
                //     title: '',slot: 'action',width: 150,align: 'center'
                // }
            ],
            total_list: [],
            show_list: [],
            // 分页
            total: 0,
            pageSize: 10,
            istio_injection: false,

            // 多选
            selecteds: [],
            // 添加容忍模态框
            show_add_toleration_modal: false,
            show_del_toleration_modal: false,
            show_add_node_affinity_modal: false,
            show_del_node_affinity_modal: false,
            show_add_pod_anti_affinity_modal: false,
            show_del_pod_anti_affinity_modal: false,
            show_edit_deploy_modal: false,

            toleration:{
                effect: 'NoSchedule',
                key: '',
                value: '',
                operator: 'Equal',
                tolerationSeconds: null,
            },
            toleration_effect_list:['NoSchedule','PreferNoSchedule','NoExecute'],
            toleration_operator_list:['Equal','Exists'],
            toleration_rules: {

            },
            // 删除容忍
            tolerations: null,
            toleration_del_index: null,

            // 添加节点亲和
            node_affinity:{
                type: 'preferred',
                match_type: 'matchExpressions',
                key: '',
                operator: 'In',
                value: '',
                weight: 1,
            },
            affinity_types:['preferred','required'],
            node_selector_types:['matchExpressions','matchFields'],
            node_affinity_operators:['In','NotIn','Exists','DoesNotExist','Gt','Lt'],
            
            node_affinity_rules:{
                key: [
                    {required: true,message:'请输入key',trigger: 'blur'}
                ],
                weight: [
                    {trigger:'blur',transform(value){return Number(value);},validator: validateWeight}
                ],
            },

            // pod互斥调度
            pod_anti_affinity:{
                type: 'preferred',
                match_type: 'matchExpressions',
                key: '',
                operator: 'In',
                value: '',
                weight: 1,
                topologyKey: "kubernetes.io/hostname",
            },

            label_selector_types:['matchExpressions','matchLabels'],
            pod_anti_affinity_operators:['In','NotIn','Exists','DoesNotExist'],
            
            pod_anti_affinity_rules:{
                key: [
                    {required: true,message:'请输入key',trigger: 'blur'}
                ],
                weight: [
                    {trigger:'blur',transform(value){return Number(value);},validator: validateWeight}
                ],
            },
            deploy_name: '',
            value: '',
            is_problem: 0,


        }
    },
    methods: {
        btn_refresh(){
            this.refresh()
        },
        submit_update_deployment(){
            console.log(this.value+update_deployment_object) 
            let name = this.deploy_name
            let data = {
                "name": name,
                "content": this.value,
            }
            k8s_namespace_post(update_deployment_object,data).then(response=>{
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                    this.$Message.success('更新资源成功')
                    this.show_edit_deploy_modal = false
                    this.refresh()
                }else {
                    alert(info)
                }
            })
        },
        btn_get_problem_deployments(){
            this.is_problem = 1
            this.refresh()
        },
        btn_edit_deploy(index){
            let name = this.show_list[index].name
            this.deploy_name = name
            let data = {
                "name": name,
            }
            k8s_namespace_post(get_deployment_object,data).then(response=>{
                let res = response.data
                let obj = res['obj']
                this.value = this.to_yaml(obj)
                this.show_edit_deploy_modal = true
            })
        },
        cacel_update_deploy(){
            this.show_edit_deploy_modal=false
        },
        to_yaml(obj){
            try{
                let yaml = YAML.stringify(obj)
                // console.log(yaml)
                return yaml
            }catch(e){
                alert(e)
            }
        },
        // 删除POD互斥
        delete_pod_anti_affinity(deploy_name){
            let data={"name":deploy_name}
            k8s_namespace_post(delete_pod_anti_affinity,data).then( (response) => {
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                    this.$Message.success('删除POD互斥调度成功')
                    this.refresh()
                }else {
                    alert(info)
                }
            })
        },
        // 删除POD互斥调度点击事件
        btn_del_pod_anti_affinity(){
            if(!this.check()) return
            let select_count = this.selecteds.length
            if (select_count > 1){
                this.$Message.error("此操作暂时不能批量")
                return false
            }

            let  name = this.selecteds[0].name
            if(this.selecteds[0].affinity == null){
                this.$Message.error("没有亲和性设置")
                return
            }
            let pod_anti_affinity = this.selecteds[0].affinity.podAntiAffinity
            if(pod_anti_affinity==null || pod_anti_affinity=='' ) {
                this.$Message.error("没有设置互斥调度")
                return
            }
            
            this.deploy_name = name
            let result = confirm("确定要删除"+name+"POD互斥调度吗(一次性清空，不管设置了几项)?")
            if(result == false) return false
            this.delete_pod_anti_affinity(name)
        },
        // 提交pod互斥事件
        submit_add_pod_anti_affinity(name){
            let validate = false
            this.$refs[name].validate((valid) => {
                if (valid) validate = true
            })
            if(!validate) return
            let pod_anti_affinity = this.pod_anti_affinity
            let deploy_name = this.deploy_name
            let data={"name":deploy_name,"pod_anti_affinity":pod_anti_affinity}
            k8s_namespace_post(add_pod_anti_affinity,data).then( (response) => {
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                    this.$Message.success('添加POD互斥调度成功')
                    this.show_add_pod_anti_affinity_modal = false
                    this.refresh()
                }else {
                    alert(info)
                }
            })
            
        },
        // 点击添加pod反亲和性事件
        btn_add_pod_anti_affinity(){
            if(!this.check()) return
            let select_count = this.selecteds.length
            if (select_count > 1){
                this.$Message.error("此操作暂时不能批量")
                return false
            }
            let  name = this.selecteds[0].name
            
            this.deploy_name = name
            // 显示模态框
            this.show_add_pod_anti_affinity_modal = true
        },
        // 删除节点亲和性
        delete_node_affinity(deploy_name){
            let data={"name":deploy_name}
            k8s_namespace_post(delete_node_affinity,data).then( (response) => {
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                    this.$Message.success('删除节点亲和成功')
                    this.refresh()
                }else {
                    alert(info)
                }
            })
        },
        // 点击删除节点亲和性事件
        btn_del_node_affinity(){
            if(!this.check()) return
            let select_count = this.selecteds.length
            if (select_count > 1){
                this.$Message.error("此操作暂时不能批量")
                return false
            }
            let  name = this.selecteds[0].name
            if(this.selecteds[0].affinity == null){
                this.$Message.error("没有亲和性设置")
                return
            }
            let node_affinity = this.selecteds[0].affinity.nodeAffinity
            if(node_affinity==null || node_affinity=='' ) {
                this.$Message.error("没有设置节点亲和性")
                return
            }
            
            this.deploy_name = name
            let result = confirm("确定要删除"+name+"节点亲和性吗(一次性清空,不管有多少项)?")
            if(result == false) return false
            this.delete_node_affinity(name)
        },
        // 提交节点亲和事件
        submit_add_node_affinity(name){
            let validate = false
            this.$refs[name].validate((valid) => {
                if (valid) validate = true
            })
            if(!validate) return

            let node_affinity = this.node_affinity
            let deploy_name = this.deploy_name
            let data={"name":deploy_name,"node_affinity":node_affinity}
            k8s_namespace_post(add_node_affinity,data).then( (response) => {
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                    this.$Message.success('添加容忍成功')
                    this.show_add_node_affinity_modal = false
                    this.refresh()
                }else {
                    alert(info)
                }
            })

        },
        // 添加添加节点亲和性事件
        btn_add_node_affinity(){
            if(!this.check()) return
            let select_count = this.selecteds.length
            if (select_count > 1){
                this.$Message.error("此操作暂时不能批量")
                return false
            }
            let  name = this.selecteds[0].name
            this.deploy_name = name
            this.show_add_node_affinity_modal = true
        } ,
        // 提交删除容忍事件
        submit_del_toleration(){
            let  toleration= this.tolerations[this.toleration_del_index]
            if (toleration.tolerationSeconds != null) {
                toleration.tolerationSeconds = parseInt( toleration.tolerationSeconds)
            }
            let data={"name":this.deploy_name,"toleration":toleration}
            k8s_namespace_post(delete_toleration,data).then( (response) => {
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                     this.$Message.success('删除容忍成功')
                    this.show_del_toleration_modal = false
                    this.refresh()
                }else {
                    alert(info)
                }
            })
        },
        // 删除容忍点击事件
        btn_del_toleration(){
            if(!this.check()) return
            let select_count = this.selecteds.length
            if (select_count > 1){
                this.$Message.error("此操作暂时不能批量")
                return false
            }
            let  name = this.selecteds[0].name
            let tolerations = this.selecteds[0].tolerations
            this.tolerations = tolerations
            console.log("即将删除容忍:",this.tolerations)
            if(tolerations==null || tolerations=='' ) {
                this.$Message.error("没有设置容忍")
                return
            }
            this.deploy_name = name
            this.show_del_toleration_modal = true
        },
        // 提交添加容忍事件 
        submit_add_toleration(){
            if (this.toleration.tolerationSeconds != null) {
                this.toleration.tolerationSeconds = parseInt( this.toleration.tolerationSeconds)
            }
            let data={
                "name":this.deploy_name,
                "toleration":  this.toleration
            }
            k8s_namespace_post(add_toleration,data).then( (response) => {
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                    this.$Message.success('添加容忍成功')
                    this.show_add_toleration_modal = false
                    this.refresh()
                }else {
                    alert(info)
                }
            })

        },
        // 点击添加容忍事件
        btn_add_toleration(){
            if(!this.check()) return
            let select_count = this.selecteds.length
            if (select_count > 1){
                this.$Message.error("此操作暂时不能批量")
                return false
            }
            let  name = this.selecteds[0].name
            this.deploy_name = name
            this.show_add_toleration_modal = true
        },
        // 点击扩缩容
        btn_update_replicas(){
            if(!this.check()) return
            let result = prompt("输入1-10副本数")
            if(result == null) return
        
            if(result=="" || ! /^[0-9]+$/.test(result)){
                alert("您输入数据为空或者含字母,请输入1-10内整数")
                return
            }
            let replicas=parseInt(result)
            // console.log(replicas)
            if(replicas < 1 || replicas >10 ){
                alert("副本数不在1-10之间")
                return
            }
            for(let i=0;i<this.selecteds.length;i++){
                let name = this.selecteds[i].name
                this.update_replicas(name,replicas)
            }
        },


        // 提交扩缩容ajax请求
        update_replicas(deploy_name,replicas){
            let data = {"name":deploy_name,'replicas':replicas}
            k8s_namespace_post(update_replicas,data).then( (response)=>{
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                    this.$Message.success('更新'+deploy_name+'副本数到'+replicas+'成功')
                    this.refresh()
                }else {
                    console.log('更新失败')
                    alert(info)
                }
            })
        },
        // 删除deployment点击事件
        btn_del_deploy(){
            if(!this.check()) return
            let result = confirm("确定要删除吗?")
            if(result == false) return false
            for(let i=0;i<this.selecteds.length;i++){
                let name = this.selecteds[i].name
                let namespace = this.selecteds[i].namespace
                delete_namespace_resource(namespace,name,delete_deploy)
                this.refresh()
            }
        },

        // 多选框选项改变就会触发
        onSelect(selection){
            this.selecteds = selection;
            console.log(this.selecteds)
        },
        // 检查有没有选项选中
        check(){
            if(this.selecteds.length == 0 ){
                this.$Message.error("请选择一项")
                return false
            }  
            return true
        },
        // 链接点击事件，进入详情页
        handleLink(params){
            let namespace = params.row.namespace
            // this.$router.push({name: 'deploymentDetail',query: {name: params.row.name,namespace: namespace}})

            const { href } = this.$router.resolve({
                name: 'deploymentDetail'
            });
            window.open(href+"?name="+params.row.name+"&namespace="+namespace, '_blank');
        },
        changePage(index) {
            let _start = (index -1) * this.pageSize
            let _end = index * this.pageSize
            this.show_list = this.total_list.slice(_start,_end)
        },
        refresh() {
            this.selecteds = []
            let url = null
            if(this.is_problem){
                url = get_problem_deployments
            }else{
                url = get_deployment_list
            }
            k8s_namespace_post(url).then( (response) => {
                show_simple_page(this,response)
            })
        },
        btn_stop_timer(){
            clearInterval(this.timer);
        },
    },
    mounted() {
        this.refresh()
        // this.timer = setInterval(this.refresh, 20000);
        // console.log(document.body.clientWidth)
    },
    // beforeDestroy() {
    //     clearInterval(this.timer);
    // }
}
</script>
<style scoped>
    pre{
        white-space: pre-wrap!important;
        word-wrap: break-word!important;
        white-space:normal!important;
        text-overflow: ellipsis;
    }
    .button-link {
        font-size:18px;
        color: black;
        padding:25px;
        line-height:60px;
    }
</style>
